package org.helidb.wroom325;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.util.Arrays;

public class SearchMethod {

	public SearchFrame frame;

	public SearchMethod(SearchFrame frame) {
		this.frame = frame;
	}

	/**
	 * 功能：Java读取txt文件的内容 步骤：1：先获得文件句柄 2：获得文件句柄当做是输入一个字节码流，需要对这个输入流进行读取
	 * 3：读取到输入流后，需要读取生成字节流 4：一行一行的输出。readline()。 备注：需要考虑的是异常情况
	 * 
	 * @param filePath
	 */
	public String readTxtFile(String filePath, String encoding) {
		try {
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				StringBuffer sb = new StringBuffer();
				int lineNumber = 0;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					lineNumber++;
					sb.append(lineNumber + ": ").append(lineTxt).append("\n");
				}
				read.close();
				return sb.toString();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return "";
	}

	boolean include(String txt, String[] keyWords) {
		for (int i = 0; i < keyWords.length; i++) {
			if (txt.indexOf(keyWords[i]) == -1) {
				return false;
			}
		}
		return true;
	}

	public void showLineNumber(String txt, String[] keyWords, String filePath) {
		String[] lines = txt.split("\n");
		for (int i = 0; i < lines.length; i++) {
			for (int j = 0; j < keyWords.length; j++) {
				if (lines[i].indexOf(keyWords[j]) != -1) {
					String content = "line " + (i + 1) + ": " + keyWords[j];
					frame.addLable(content, filePath, i + 1);
				}
			}

		}
	}

	/**
	 * 展示与题目关键词相匹配的字幕文件以及关键词所在行数，以方便查阅字幕文件<br>
	 * 结果输出到控制台
	 * 
	 * @param path
	 *            字幕的存放目录
	 * @param keyWords
	 *            包含题目关键词的String数组
	 * @param num
	 *            搜索到第多少集
	 */
	public void showMatchResult(String path, String[] keyWords, final String[] exts, final String encoding) {
		File file = new File(path);
		File[] childrenPath = file.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}
		});
		for (File child : childrenPath) {
			showMatchResult(child.getPath(), keyWords, exts, encoding);
		}
		String[] strTxts = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				for (String ext : exts) {
					if (name.endsWith(ext)) {
						return true;
					}
				}
				return false;
			}
		});
		// System.out.println(Arrays.toString(strTxts));
		for (int i = 0; i < strTxts.length; i++) {

			String filePath = path + "/" + strTxts[i];
			String txt = readTxtFile(filePath, encoding);

			if (include(txt, keyWords)) {
				frame.addLable(strTxts[i] + " - " + filePath);

				showLineNumber(txt, keyWords, filePath);

				frame.addLable(" ");
				// System.out.println(txt);
			}
		}
	}

}
